package webjoch.process.strategies.puzzles;

import java.awt.Graphics2D;

import com.rsbuddy.event.events.MessageEvent;
import com.rsbuddy.script.methods.Camera;
import com.rsbuddy.script.methods.Menu;
import com.rsbuddy.script.methods.Mouse;
import com.rsbuddy.script.methods.Objects;
import com.rsbuddy.script.task.Task;
import com.rsbuddy.script.util.Filter;
import com.rsbuddy.script.wrappers.GameObject;
import com.rsbuddy.script.wrappers.Tile;

import webjoch.process.strategies.Puzzle;
import webjoch.wrappers.Room;
/**
 * 
 * @author Webjoch
 *
 */
public class Levers extends Puzzle{
	Room room;
	boolean clicked = false;
	
	Tile currentLeverLocation = null;
	
	public final int LEVER = 49381;
	//public final int LEVER_PULLED = ;
	
	@Override
	public void setRoom(Room room) {
		this.room = room;
	}

	@Override
	public boolean isValid() {
		return room.getObject(LEVER) != null;
	}

	@Override
	public Type getType() {
		return Type.LEVERS;
	}

	@Override
	public int loop(){
		if (Camera.getPitch() > 10){
			Camera.setPitch(false);
			return 1;
		}
		final GameObject nearestLever = room.getObject(LEVER);
		if (currentLeverLocation == null) currentLeverLocation = nearestLever.getLocation();
		
		final GameObject secondNearestLever = Objects.getNearest(new Filter<GameObject>(){
			public boolean accept(GameObject o) {
				return room.getArea().contains(o.getLocation()) && o.getId() == LEVER && !o.getLocation().equals(currentLeverLocation);
			}
		});
		
		if (!clicked){
			Camera.turnTo(nearestLever);
			if (nearestLever.interact("Pull")){
				clicked = true;
				return 1;
			}
		}
		Camera.turnTo(secondNearestLever); Task.sleep(200);
		secondNearestLever.hover();
		Mouse.click(false);
		if (Menu.isOpen() && Menu.getActions().length > 1){
			while(room.getObject(LEVER).getLocation().equals(currentLeverLocation)){
				Task.sleep(25);
			}
			currentLeverLocation = null;
			Menu.click(0);
		}
		return 0;
	}

	@Override
	public int[] itemNeed() {
		return new int[0];
	}

	@Override
	public boolean isFinished() {
		return false;
	}

	@Override
	public void messageReceived(MessageEvent e) {
		
	}

	@Override
	public void onRepaint(Graphics2D g) {
		// TODO Auto-generated method stub
		
	}

}
